﻿using GameLogic;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarSphere : OWObject
{
    public GameConfig.map.PlaceType PlaceType;
    public float RotationSelfVelocity = 1;
    public float RotationCenterVelocity = 1;
    public GameObject ParentSphere;
    public GameObject Model;
    public Vector3 SelfRotateDirection = new Vector3(0, 1, 1);
    public Vector3 ParentRotateDirection = new Vector3(0, 0, 1);

    public float distance = 0;
    public float angle = 0;
    public Rigidbody2D m_Rigidbody;
    void Start()
    {
        //m_Rigidbody = GetComponent<Rigidbody2D>();
        if (ParentSphere)
        {
            var parentPos = new Vector3(ParentSphere.transform.position.x, ParentSphere.transform.position.y, 0);
            var selfPos = new Vector3(transform.position.x, transform.position.y, 0);
            distance = (parentPos - selfPos).magnitude;
        }
    }

    // Update is called once per frame
    void Update()
    {
    }
    private void FixedUpdate()
    {
        float ifps = Time.fixedDeltaTime;
        if (Model)
        {
            if (m_Rigidbody)
                m_Rigidbody.MoveRotation(Quaternion.AngleAxis(RotationSelfVelocity * ifps, SelfRotateDirection));
            else
                transform.rotation *= Quaternion.AngleAxis(RotationSelfVelocity * ifps, SelfRotateDirection);
        }

        if (ParentSphere)
        {
            angle += ifps * RotationCenterVelocity;
            if (angle > 360)
                angle -= 360;
            var newVec = Quaternion.AngleAxis(angle, ParentRotateDirection) * (Vector3.up * distance);
            newVec = ParentSphere.transform.position + newVec;
            //if (owType == Type.Planet)
            //    newVec.z = 1000;
            transform.position = newVec;

            if (m_Rigidbody)
                m_Rigidbody.MovePosition(newVec - transform.position);
            else
                transform.position = newVec;
        }
    }
    void UpdateAngle()
    {
        angle = RotationCenterVelocity * (GameDataModule.Instance.Time / 1000f);
    }
}
